package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"video-sys/handler"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {

	r := gin.Default()

	r.Use(Cors())
	r.Static("/static", "./static")

	userGroup := r.Group("/user")
	{
		userGroup.POST("/user_add", handler.UserAdd)
		userGroup.POST("/login", handler.Login)
	}
	moviesGroup := r.Group("/movies")
	{
		moviesGroup.POST("/add", handler.Add)
		moviesGroup.POST("/upload", handler.Upload)
		moviesGroup.GET("like_list", handler.LikeList)
		moviesGroup.GET("gun_list", handler.GunList)
		moviesGroup.GET("/list", handler.List)
	}
	return r
}
